/**
 * @File name: driver/serial.h
 * @Synopsis:
 * @Author: xing.dai@aliyun.com
 * @Version: 0.0.1
 * @Date: 2018-08-30 18:42
 */

#ifndef __SERIAL_H__
#define __SERIAL_H__

#include "device.h"

typedef struct _serial_config {
    uint32 baud_rate;
    uint32 data_bits	: 4;
    uint32 stop_bits	: 2;
    uint32 parity		: 2;
    uint32 bit_order	: 1;
    uint32 invert		: 1;
    uint32 bufsz		: 16;
    uint32 reserved		: 4;
} serial_config_t;

typedef struct _serial_device {
    device_t device;
    const struct _serial_ops *ops;
    serial_config_t config;

    void *serial_rx;
    void *serial_tx;
} serial_device_t;

typedef const struct _serial_ops {
    int (*configure)(serial_device_t *serial, serial_config_t *config);
    int (*control)(serial_device_t *serial, int cmd, void *arg);

    int (*putc)(serial_device_t *serial, char c);
    int (*getc)(serial_device_t *serial);

    uint32 (*dma_transmit)(serial_device_t *serial, char *buf, uint32 size, int direction);
} serial_ops_t;

#define BAUD_RATE_9600		    9600
#define BAUD_RATE_115200		115200

#define DATA_BITS_8			8
#define STOP_BITS_1			0
#define STOP_BITS_2			1
#define STOP_BITS_3			2
#define STOP_BITS_4			3
#define PARITY_NONE			0
#define PARITY_ODD			1
#define PARITY_EVEN			2
#define BIT_ORDER_LSB		0
#define BIT_ORDER_MSB		1
#define NRZ_NORMAL			0	/* Non Return to Zero: normal mode*/
#define NRZ_INVERTED		1	/* Non Return to Zero: inverted mode*/

#define SERIAL_RB_BUFSZ		64

#define SERIAL_CONFIG_DEFAULT	\
{								\
	BAUD_RATE_115200,			\
	DATA_BITS_8,				\
	STOP_BITS_1,				\
	PARITY_NONE,				\
	BIT_ORDER_LSB,				\
	NRZ_NORMAL,					\
	SERIAL_RB_BUFSZ,			\
	0							\
}


int hw_serial_register(serial_device_t *serial, char *name, uint32 flag, void *data);


#endif

